import { Component, OnInit } from '@angular/core';
import { BladeContainerBase } from "../../../../shared/blade/blade-container-base";
import { MenuParam } from "../../../../entity/menu-param.model";
import { MenuParamService } from "../../../../service/menu_param.service";
import { Command } from "../../../../shared/toolbar.service";
import { Observable } from "rxjs/Rx";
import { Blade } from "../../../../shared/blade/blade.model";
import { BladeData } from "../../../../shared/blade/blade-data";
import { MenuParamEditComponent } from "../menu-param-edit/menu-param-edit.component";
import { DialogResult } from "../../../../shared/dialog.service";

@Component({
  selector: 'app-menu-param-list',
  templateUrl: './menu-param-list.component.html',
  styleUrls: ['./menu-param-list.component.css']
})
export class MenuParamListComponent extends BladeContainerBase<MenuParam>{
  menuParams: MenuParam[] = [];
  constructor(
    private menuParamService: MenuParamService) {
    super();
  }

  ngOnInit() {
    this.commands = [
      new Command("add", "添加", "fa fa-plus", "setofkey:insert", blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
      new Command("delete", "删除", "fa fa-trash-o", "setofkey:delete", blade => this.exeDelete(blade), blade => this.canDelete(blade))
    ];
    super.ngOnInit();
  }
  /**
 * 初始化数据
 * 
 * 
 * @memberof DicKeyListComponent
 */
  initData(): any | Observable<any> | Promise<any> {
    
    this.menuParamService
      .getMenuParamsByMenuId(this.parentEntity.menuId)
      .then(res => {
        if (res ) { this.menuParams = [...res]; };
      })
      .catch(error => this.bladeData.error = error)
      .then(res => this.bladeData.isLoading = false);
  }
  exeAdd(blend: Blade): void {
    let data: BladeData = new BladeData();
    data.id = "dicValueCreate";
    data.title = "菜单参数创建";
    data.subtitle = "创建一个菜单参数";
    data.isClosingDisabled = false;
    let createValueBlade = this.bladeService.createBlade(MenuParamEditComponent, data, true, null, this.parentEntity);
    this.bladeService.showBlade(createValueBlade, this.currentBlade);
  }
  canExeAdd(blend: Blade): boolean {
    return true;
  }
  exeDelete(blend: Blade): void {
    let selecteds = this.getSelected();
    if (selecteds.length > 0) {
      this.dialogService
        .showConfirmationDialog("确认删除", "是否要删除选中项？")
        .subscribe(dialogResult => {
          if (dialogResult === DialogResult.Ok) {
            this.currentBlade.bladeData.isLoading = true;
            this.menuParamService.batchDelete(selecteds.map(o => o.paramId))
              .then(res => {
                if (res) {
                  this.refresh(undefined);
                }
              })
              .catch(error => this.bladeData.error = error)
              .then(res => this.bladeData.isLoading = false);
          }
        });

    }
  }
  canDelete(blend?: Blade): boolean {
    return this.getSelected().length > 0;
  }
  /**
   * 获得选中项
   * 
   * @returns {Setofkey[]} 
   * 
   * @memberof DicKeyListComponent
   */
  getSelected(): MenuParam[] {
    if (this.menuParams.length > 0) {
      let filter = this.menuParams.filter(ref => ref.isChecked)
      return filter == null ? [] : filter;
    }
    return [];
  }
  /**
     * 单选
     * 
     * @param {any} event 
     * @param {any} key 
     * 
     * @memberof DicKeyListComponent
     */
  onItemCheack(event: HTMLInputElement, value: MenuParam) {
    value.isChecked = event.checked;
  }
  /**
   * 全选/全不选
   * 
   * @param {any} event 
   * 
   * @memberof DicKeyListComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.menuParams.length > 0) {
      for (let i = 0; i < this.menuParams.length; i++) {
        this.menuParams[i].isChecked = event.checked;
      }
    }
  }
  onSeletedChange(selectedValue: MenuParam) {
    this.showEditBlade(selectedValue);

  }
  /**
   * 展开数据字典值编辑弹出层
   * 
   * @param {Setofkey} selectedKey 
   * 
   * @memberof DicKeyListComponent
   */
  showEditBlade(selected: MenuParam) {
    if (this.currentEntity != selected
      || this.bladeService.findBlade("menuParamEdit") == undefined) {
      let data: BladeData = new BladeData();
      data.id = "menuParamEdit";
      data.title = selected.paramName;
      // data.subtitle = selected.paramName;
      data.isClosingDisabled = false;
      let dicValueEditBlade = this.bladeService.createBlade(MenuParamEditComponent, data, false, selected, this.parentEntity);
      this.bladeService.showBlade(dicValueEditBlade, this.currentBlade).then(res => {
        if (res) {
          this.currentEntity = selected;
        }
      });
    }
  }

}



// WEBPACK FOOTER //
// ./src/app/module/admin/menu/menu-param-list/menu-param-list.component.ts